package cj.web.marry.controllers;

import java.util.EnumSet;
import java.util.List;
import java.util.Map;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.Constants;
import cj.utils.DateUtils;
import cj.utils.ReturnUUID;
import cj.utils.sms.SendSMSUtil;
import cj.utils.weixin.HttpsWenXinUtil;
import cj.web.marriage.constants.MarriagePersionTypeEnum;
import cj.web.marriage.constants.MarriageRegStatusEnum;
import cj.web.marriage.constants.MarriageRegTypeEnum;
import cj.web.marriage.constants.RegAddressEnum;
import cj.web.marry.dao.AppTokenDAO;
import cj.web.marry.dao.RegDAO;

//新结婚预约功能
@Path("marryreg")
public class MarryRegController {
	
	@Autowired
	private RegDAO regDAO; 
	@Autowired
	private AppTokenDAO appTokenDao;
	
	//----------------------------------------首页	选择
	/**
	 * 结婚预约协议Code
	 * @param model
	 * @param code
	 * @return
	 */
	@Get("marryAgreementCode")
	public String marryAgreementCode(Model model,@Param("code")String code)
	{
		String openId = HttpsWenXinUtil.retOpenId(code,Constants.weixinkey.AppID,Constants.weixinkey.AppSecret);
		model.add("openId", openId);
		return "r:marryAgreement?openId="+openId;
	}
	
	/**
	 * 结婚预约协议选择界面	预约&查询
	 * @param model
	 * @param openId
	 * @return
	 */
	@Get("marryAgreement")
	public String marryAgreement(Model model,@Param("openId")String openId)
	{
		model.add("openId", openId);
		return "/views/marry/newmarry/marryAgreement.vm";
	}
	
	/**
	 * 结婚预约协议详情
	 * @param model
	 * @param openId
	 * @return
	 */
	@Get("marryAgreementInfor")
	public String marryAgreementInfor(Model model,@Param("openId")String openId)
	{
		model.add("openId", openId);
		return "/views/marry/newmarry/marryAgreementInfo.vm";
	}
	
	//----------------------------------------预约界面
	/**
	 * 我要预约界面	选择 男方类型，女方类型
	 * @param model
	 * @param openId
	 * @return
	 */
	@Get("marryReg")
	public String marryReg(Model model,@Param("openId")String openId)
	{
		model.add("openId", openId);
		return "/views/marry/newmarry/marryReg.vm";
	}
	
	/**
	 * 用户选择后，根据不同的身份，显示不同的材料提示信息
	 * @param model
	 * @param openId
	 * @param manType		男方类型
	 * @param womanType		女方类型
	 * @return
	 */
	@Get("marryRegInfo")
	public String marryRegInfo(Model model,@Param("openId")String openId,
			@Param("manType")int manType,@Param("womanType")int womanType)
	{
		model.add("openId", openId);
		model.add("manType", manType);
		model.add("womanType", womanType);
		
		//0:居民	1:军人
		int ndjm = MarriagePersionTypeEnum.ndjm.getValue();
		int xyjr = MarriagePersionTypeEnum.xyjr.getValue();
		
		
		if(ndjm==manType && ndjm==womanType){
			//manType=0	& womanType=0	男女当事人均为内地居民
			return "/views/marry/newmarry/marryRegInfo00.vm";
		}else if(xyjr==manType && ndjm==womanType){
			//manType=1	& womanType=0	男方当事人为现役军人，女方当事人为内地居民
			return "/views/marry/newmarry/marryRegInfo10.vm";
		}else if(xyjr==manType && xyjr==womanType){
			//manType=1	& womanType=1	男女双方当事人均为现役军人
			return "/views/marry/newmarry/marryRegInfo11.vm";
		}else if(ndjm==manType && xyjr==womanType){
			//manType=0	& womanType=1	男方当事人为内地居民，女方当事人为现役军人
			return "/views/marry/newmarry/marryRegInfo01.vm";
		}else{
			return "/views/marry/newmarry/marryRegInfo00.vm";
		}
	}
	
	/**
	 * 我的预约界面
	 * @param model
	 * @param openId
	 * @param type			预约类型
	 * @param manType		男方类型
	 * @param womanType		女方类型
	 * @return
	 */
	@Get("myMarryRegPage")
	public String myMarryRegPage(Model model,@Param("openId")String openId,
			@Param("manType")int manType,@Param("womanType")int womanType)
	{
		if (openId !=null && openId.length() > 0 ) {
			model.add("AddressEnum", EnumSet.allOf(RegAddressEnum.class));
			model.add("type", MarriageRegTypeEnum.jiehunyy.getValue());
			model.add("womanType", womanType);
			model.add("manType", manType);
			//获取幸福码
			String code = regDAO.getCode(openId);
			if(code != null && code.length()>0){
				model.add("code",code);
			}else{
				return "/views/marry/newmarry/fail.vm";
			}			
			
			return "/views/marry/newmarry/marryRegDetail.vm";
		}
		
		return "/views/marry/newmarry/fail.vm";
	}
	
	/**
	 * 发送验证码
	 * @param phone	手机号
	 * @return
	 */
	@Post("createLoginToken")
	public String createLoginToken(@Param("link") String link) {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		String accountSid="8aaf0708559f32dd0155a061b0e70089";
		String accountToken="ca8d98f8170b4293be592df629a8ea07";
		String appId="8aaf070855ed9ef10155fd08aee90737";
		String tempid="102126";//短信模版ID
		String token= ReturnUUID.getRandomNum();//验证码
		String params2="1";//时间参数
		
		String[] msg = new String[]{token,params2};
		
		//调用短信平台
		String rep = SendSMSUtil.sendSMS(accountSid, accountToken, appId, link, tempid, msg);
		if(rep.equals(Constants.returnObj.returnFlag_true))
		{
			appTokenDao.updateToken(link);
			appTokenDao.createToken(link, token);
		}
		else
		{
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();	
	}
	
	/**
	 * 提交预约信息
	 * @param code			幸福码
	 * @param man			男方姓名
	 * @param woman			女方姓名
	 * @param regTime		预约时间
	 * @param link			联系人电话
	 * @param token			验证码
	 * @param type			预约类型
	 * @param addressId		地址ID
	 * @param manType		男方类型
	 * @param womanType		女方类型
	 * @return
	 */
	@Post("submit")
	public String submit(@Param("code")String code, @Param("man")String man, @Param("woman")String woman, @Param("manType")int manType, 
			@Param("womanType")int womanType, @Param("regTime")String regTime, @Param("link")String link,
			@Param("token")String token, @Param("type")String type, @Param("addressId")String addressId,
			@Param("man_city")String man_city, @Param("man_police")String man_police,
			@Param("woman_city")String woman_city, @Param("woman_police")String woman_police){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		if(appTokenDao.queryToken(link, token) == 0){
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
			return "@" + jo.toString();
		}
		
		//查询预约信息
		List<Map> replis = regDAO.repeatRegByLink(link);
		
		if(replis!=null && replis.size()>0){
			//存在
			String l_regTime = replis.get(0).get("regTime").toString();
			//和当前时间做比较是否超过	0:相等	1:大于当前时间	-1:小于当前时间
			int value = DateUtils.compareDateWithNow(DateUtils.parse(l_regTime,DateUtils.DATE_SMALL_STR));
			if(1==value){
				//超过时间
				jo.put("link", link);
				jo.put("returnFlag", Constants.returnObj.returnFlag_repeat);
				return "@" + jo.toString();
			}
		}
		
		int status = MarriageRegStatusEnum.yuyue.getValue();
		try {
			
			String manRegistry= man_city+" "+man_police;
			String womanRegistry= woman_city+" "+woman_police;
			
			regDAO.createNew(code, man, "", woman, "", regTime, link, addressId, type, status, manType, womanType,manRegistry,womanRegistry);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@" + jo.toString();
	}
	
	/**
	 * 颁证仪式预约
	 * @param model
	 * @param code code获取openId
	 * @return
	 */
	@Get("crtCode")
	public String crtCode(Model model, @Param("code")String code){
		String openId = HttpsWenXinUtil.retOpenId(code,Constants.weixinkey.AppID,Constants.weixinkey.AppSecret);
//		String openId = "ofwuNsyVHbXdpotCwZ_ZJLp7DSrQ";
		
		return "r:certification?openId=" + openId;
	}
	
	@Get("certification")
	public String certification(Model model, @Param("openId")String openId){
		model.add("openId", openId);
		return "/views/marry/newmarry/certification.vm";
	}
	
	@Get("crtDetail")
	public String crtDetail(Model model, @Param("type")int type){
		if(type == 0){        //普通颁证
			model.add("fileName","putong_example");
			model.add("title","普通颁证仪式");
		}
		else if(type == 1){   //个性颁证
			model.add("fileName","gexing_example");
			model.add("title","个性颁证仪式");
		}
		else if(type == 2){   //私人定制颁证
			model.add("fileName","dingzhi_example");
			model.add("title","私人定制颁证仪式");
		}
		return "/views/marry/newmarry/certificationDetail.vm";
	}
	
	@Get("success")
	public String success(){
		return "/views/marry/newmarry/success.vm";
	}
	
	@Get("fail")
	public String fail(){
		return "/views/marry/newmarry/fail.vm";
	}

	//----------------------------------------预约查询
	/**
	 * 我要预约界面	选择 男方类型，女方类型
	 * @param model
	 * @param openId
	 * @return
	 */
	@Get("queryRegDetail")
	public String queryRegDetail(Model model,@Param("openId")String openId)
	{
		model.add("openId", openId);
		return "/views/marry/newmarry/queryRegDetail.vm";
	}
	
	/**
	 * 预约查询
	 * @param model
	 * @param link	手机号
	 * @return
	 */
	@Post("queryReg")
	public String queryReg(Model model,@Param("link")String link){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		List<Map> replis = regDAO.queryRegByLink(link);
		
		if(replis!=null && replis.size()>0){
			
			int status = Integer.valueOf(replis.get(0).get("status").toString());
			String memo = replis.get(0).get("memo")==null?"":replis.get(0).get("memo").toString();
			jo.put("memo", memo);
			if(MarriageRegStatusEnum.yuyue.getValue()==status){
				//预约
//				return "/views/marry/newmarry/checking.vm";
				jo.put("status", "checking");
			}else if(MarriageRegStatusEnum.agree.getValue()==status){
				//审核通过
//				return "/views/marry/newmarry/checkSuccess.vm";
				jo.put("status", "checkSuccess");
			}else{
				//审核不通过
//				return "/views/marry/newmarry/checkFailed.vm";
				jo.put("status", "checkFailed");
			}
		}else{
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@" + jo.toString();
	}

	/**
	 * 审核中
	 * @return
	 */
	@Get("checking")
	public String checking(){
		return "/views/marry/newmarry/checking.vm";
	}
	
	/**
	 * 审核通过
	 * @return
	 */
	@Get("checkSuccess")
	public String checkSuccess(Model model,@Param("memo")String memo){
		model.add("memo", memo);
		return "/views/marry/newmarry/checkSuccess.vm";
	}
	
	/**
	 * 审核失败
	 * @param model
	 * @param memo
	 * @return
	 */
	@Get("checkFailed")
	public String checkFailed(Model model,@Param("memo")String memo){
		model.add("memo", memo);
		return "/views/marry/newmarry/checkFailed.vm";
	}

}
